# Contributor: Marian Buschsieweke <marian.buschsieweke@posteo.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=py3-litex-hub-modules
_pkgname=litex
pkgver=2024.04
pkgrel=0
pkgdesc="Python modules containing verilog and/or data files for use with LiteX"
url="https://github.com/litex-hub"
license="BSD-2-Clause"
arch="all"
# match this commit against https://github.com/litex-hub/pythondata-software-picolibc/tree/master/pythondata_software_picolibc
_picolibc_commit="f165dc22f1f67e3e8bdc8edf750ff7dc596de2ff"
_fpnew_commit="855bb82b6e85772fc290fa8b9c14fdd8f1b16be7"
_riscv_dbg_commit="6d38d957b036231db668666255e938c91b7ce424"
_trace_debugger_commit="0aafa398e208ad79826407e3805642987287cfae"
_common_cells_commit="790f2385c01c83022474eede55809666209216e3"
_fpu_div_sqrt_mvp_commit="83a601f97934ed5e06d737b9c80d98b08867c5fa"
_modules="
	pythondata-cpu-blackparrot:2024.04:BSD-3-Clause
	pythondata-cpu-cv32e40p:2024.04:Apache-2.0
	pythondata-cpu-cv32e41p:2024.04:Apache-2.0
	pythondata-cpu-cva5:2024.04:Apache-2.0
	pythondata-cpu-cva6:2024.04:Apache-2.0
	pythondata-cpu-ibex:2024.04:Apache-2.0
	pythondata-cpu-lm32:2024.04:EPL-1.0
	pythondata-cpu-marocchino:2024.04:MPL-2.0
	pythondata-cpu-microwatt:2024.04:CC-BY-SA-4.0
	pythondata-cpu-minerva:2024.04:BSD-2-Clause
	pythondata-cpu-mor1kx:2024.04:MPL-2.0
	pythondata-cpu-naxriscv:2024.04:MIT
	pythondata-cpu-picorv32:2024.04:ISC
	pythondata-cpu-rocket:2024.04:Apache-2.0
	pythondata-cpu-serv:2024.04:ISC
	pythondata-cpu-vexriscv:2024.04:MIT
	pythondata-cpu-vexriscv_smp:2024.04:MIT
	pythondata-misc-tapcfg:2024.04:LGPL-2.1-only
	pythondata-software-compiler_rt:2024.04:Apache-2.0
	pythondata-software-picolibc:2024.04:BSD-3-Clause
	valentyusb:2023.12:BSD-3-Clause
	"
depends="
	python3
	"
makedepends="
	py3-setuptools
	py3-gpep517
	py3-wheel
	py3-installer
	"
subpackages="$pkgname-pyc"
source="
	picolibc-$_picolibc_commit.tar.gz::https://github.com/picolibc/picolibc/archive/$_picolibc_commit.tar.gz
	fpnew-$_fpnew_commit.tar.gz::https://github.com/antmicro/fpnew/archive/$_fpnew_commit.tar.gz
	riscv-dbg-$_riscv_dbg_commit.tar.gz::https://github.com/antmicro/riscv-dbg/archive/$_riscv_dbg_commit.tar.gz
	trace_debugger-$_trace_debugger_commit.tar.gz::https://github.com/antmicro/trace_debugger/archive/$_trace_debugger_commit.tar.gz
	common_cells-$_common_cells_commit.tar.gz::https://github.com/pulp-platform/common_cells/archive/$_common_cells_commit.tar.gz
	fpu_div_sqrt_mvp-$_fpu_div_sqrt_mvp_commit.tar.gz::https://github.com/pulp-platform/fpu_div_sqrt_mvp/archive/$_fpu_div_sqrt_mvp_commit.tar.gz

	0001-fix-pythondata_software_compiler_rt-python3.patch
	0002-fix-pythondata-cpu-blackparrot-python3.patch
	"
builddir="$srcdir"
# most LiteX-Hub repos contain no unit tests. The basically wrap upstream
# repos in a fashion suitable for use with LiteX, so they can rely on unit
# tests of the upstream repos. Those LiteX-Hub repos that do have tests
# depend on LiteX to be installed, but LiteX depends on these for its unit
# tests (circular dependency). The unit tests in LiteX are more important, so
# we disable tests here.
options="!check"

# The data contains ELF files intended for the softcore CPUs, which cannot
# be stripped with the native toolchain due to mismatching target architecture.
options="$options !strip"

_repo_name() {
	echo ${1%%:*}
}

_pkgver() {
	a=${1%:*}
	echo ${a#*:}
}

_version_by_name() {
	for _name_ver_license in $_modules; do
		if [ "$1" = "$(_repo_name "$_name_ver_license")" ]; then
			_pkgver "$_name_ver_license"
		fi
	done
}

_supply_git_submodule() {
	rmdir "$1" || rm "$1"
	ln -s "$2" "$1"
}

_license() {
	echo $1 | cut -d : -f 3
}

for _name_ver_license in $_modules; do
	_name="$(_repo_name "$_name_ver_license")"
	_ver="$(_pkgver "$_name_ver_license")"
	source="$source $_name-$_ver.tar.gz::https://github.com/litex-hub/$_name/archive/refs/tags/$_ver.tar.gz"
	subpackages="$subpackages py3-litex-hub-$_name:_subpkg"
done

prepare() {
	local _name
	local _ver
	default_prepare

	# remove drivers for OSX, Solaris and Windows as well as tools for mingw.
	# We don't need them and they cause lots of warnings
	rm -r "$srcdir"/pythondata-misc-tapcfg-2024.04/pythondata_misc_tapcfg/data/drivers
	rm -r "$srcdir"/pythondata-misc-tapcfg-2024.04/pythondata_misc_tapcfg/data/scons-tools

	# setuptools.find_packages() doesn't work here and many instance of
	#     Warning: Package '<data_pkg_name>' is absent from the packages configuration.
	# are triggered. Using setuptools.find_namespace_packages() instead does
	# the trick.
	find "$srcdir" -maxdepth 2 -name setup.py -exec \
		sed \
			-e 's/packages=setuptools\.find_packages/packages=setuptools\.find_namespace_packages/g' \
			-i {} \;

	# manually supply pythondata-software-picolibc git submodule from downloaded source
	_name=pythondata-software-picolibc
	_ver="$(_version_by_name "$_name")"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_software_picolibc/data" \
		"$srcdir/picolibc-$_picolibc_commit"

	# manually supply pythondata-cpu-cv32e40p git submodule from downloaded source
	_name=pythondata-cpu-cv32e40p
	_ver="$(_version_by_name "$_name")"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/fpnew" \
		"$srcdir/fpnew-$_fpnew_commit"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/riscv-dbg" \
		"$srcdir/riscv-dbg-$_riscv_dbg_commit"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/trace_debugger" \
		"$srcdir/trace_debugger-$_trace_debugger_commit"

	# manually supply git submodules for fpnew
	_supply_git_submodule \
		"$srcdir/fpnew-$_fpnew_commit/src/common_cells" \
		"$srcdir/common_cells-$_common_cells_commit"
	_supply_git_submodule \
		"$srcdir/fpnew-$_fpnew_commit/src/fpu_div_sqrt_mvp" \
		"$srcdir/fpu_div_sqrt_mvp-$_fpu_div_sqrt_mvp_commit"
}

build() {
	local _name
	local _version
	for _name_ver_license in $_modules; do
		_name="$(_repo_name "$_name_ver_license")"
		_version="$(_pkgver "$_name_ver_license")"
		echo "--> Building LiteX Hub module $_name"

		cd "$srcdir/$_name-$_version"
		gpep517 build-wheel \
			--wheel-dir "$srcdir/.dist" \
			--output-fd 3 3>&1 >&2
	done
}

package() {
	# This package will be empty after all the subpackage split functions are
	# executed. We use it as a meta-package to pull in all the subpackage for
	# anyone interested in a full LiteX installation.
	for _name_ver_license in $_modules; do
		_name="$(_repo_name "$_name_ver_license")"
		depends="$depends py3-litex-hub-$_name=$pkgver-r$pkgrel"
	done

	local wheel
	mkdir -p "$pkgdir"

	for wheel in "$srcdir/.dist"/*.whl; do
		python3 -m installer -d "$pkgdir" \
			"$wheel"
	done
}

_subpkg() {
	local _name
	local _pyname
	_name="${subpkgname#py3-litex-hub-}"
	_pyname="${_name//-/_}"
	amove usr/lib/python3*/site-packages/"$_pyname"
	amove usr/lib/python3*/site-packages/"$_pyname"-*.dist-info

	for _name_ver_license in $_modules; do
		if [ "$_name" = "$(_repo_name "$_name_ver_license")" ]; then
			license="$(_license "$_name_ver_license")"
			pkgdesc="Python module containing verilog/data files \"$_name\" for use with LiteX"
		fi
	done
}

sha512sums="
795a9983d1bc0f851d8f1116e0c580a893aefa82d2034796d14868a1ec14f2ec6d2a4f5bf649b073b8e59f903bd65642766580c52573c7064ba3fb6b972e6e07  picolibc-f165dc22f1f67e3e8bdc8edf750ff7dc596de2ff.tar.gz
66b088bda0a629af97ad109ba06a7d0f9da7f2fb515c6de9752862c9ff77db9c23e520c5bffbb98c3ecc6fb2f800ea266f8b8359d3ad3c3ea2c970490877d2e1  fpnew-855bb82b6e85772fc290fa8b9c14fdd8f1b16be7.tar.gz
534a1c2358c85ed7464971b6cd2f06d949ec1db918ed531a75e0b5fe6da9a1b86f9cb675f1fb400cec9c5d607ef40bc0320a6e38b71650f1d0f4111d1c41dd02  riscv-dbg-6d38d957b036231db668666255e938c91b7ce424.tar.gz
3979457f51be8d1b079feaa07e04b02b3db425e1e02a52ae46c1fd4d304c2b513cf49d180c6018fe01e85908523712ce069de2893f3d180019264de42f6c619d  trace_debugger-0aafa398e208ad79826407e3805642987287cfae.tar.gz
926856d4ba701a14c29bf4fe7d9cec774b95fe8778bb0029e1aa361e144a99f328cf4f0d5806a7dee21a2e152a662b9aa7e9bb45b84c816280e0b6ac943979fc  common_cells-790f2385c01c83022474eede55809666209216e3.tar.gz
d7bf78ddbb00239a37e6410994bfc75c07a295f44aa0ecf968949448433a05282c7279f5d46b77a12faa77bba2cac92251cb56f972a95b22443c6045feba49db  fpu_div_sqrt_mvp-83a601f97934ed5e06d737b9c80d98b08867c5fa.tar.gz
b24680cd315482c9eee19d313e3d6800d7bc6ce720e3ebc156c3d2f7e7c0866ad6c78c21045d9bd807a18465b147454560cbcc262ece902de4cd7219224e8ab1  0001-fix-pythondata_software_compiler_rt-python3.patch
a8fc7d74bb8e9ad46f4c440ba9ab732750ba7698893f24ddd6239e73ed3988c593fcb2f8d47711f29c5cdf237b1834f3910a4f9e947cf1b5cae2f1a768d3553c  0002-fix-pythondata-cpu-blackparrot-python3.patch
cf3fcae38128ddad0dcaf2cb56934088f3d353d53ae8418d951ac37ab4c18a1a53ccebaa9317d544c7cccb60b74ae4e78b820b9fd2487295360628072126596b  pythondata-cpu-blackparrot-2024.04.tar.gz
977bad3910950526861cd64715079fc5c595440464cf4c77c912e718db6f7b0dfc1414eed1b4c96fd40f728176fa3d3e925ff6f2af827900e0840b462dcb874c  pythondata-cpu-cv32e40p-2024.04.tar.gz
7da019e5b753c1f4892df17e8c311832cef4fba86524151d261cb3b2dbd69e57a2891c66d10931e7adf3e6a3d9c5044bf143c39a47686dd1e22c217c5d5360eb  pythondata-cpu-cv32e41p-2024.04.tar.gz
21b7d740705ecac4df5b1fd8064ef81b1d4f3365aa69df14f093b6ffcc2aaa5b0a91a0133aab6bd35e37261bf44d47dbf30e19eb41aae78b0c3babc111939673  pythondata-cpu-cva5-2024.04.tar.gz
f38e0b1f4aef8677b15d02e3e32cbaa3952930f2d1a66ef16f0d3e6660930929112a149520362fd6be1a3bdc172e4686b45779a0e68a7110caff6297539fea3b  pythondata-cpu-cva6-2024.04.tar.gz
216361ef9f9e903f8151d0310766b9bbf249ccae160972d7f3602bfef0794121e220e0224526cff3462de248a9779423e4be49f30c1e53685cde6a9f0ca5ef1a  pythondata-cpu-ibex-2024.04.tar.gz
537b44e653f71008258d33b22ab01a6b159b67ce498351ea8b845e05044c4c6e3235983777d57ff4a15387c275d1890c287b5d82be5b8ca116597c515f91b09b  pythondata-cpu-lm32-2024.04.tar.gz
ac623301e0a7428d24c8fcd2249c5237a0acb20181f188a0ed832f03101d978d885cf6769bd7781db2e7691e3012041aa414544d0d653c5ee64b06337e9f78cc  pythondata-cpu-marocchino-2024.04.tar.gz
3c0673277a5d9073fa6b73a91a3d588e46a5cb75733908b43dfa4343e6a49083bf78b85cc807c013ecfc45420159f6d6ea9428cff5540a8e8f7aacb1ba15e886  pythondata-cpu-microwatt-2024.04.tar.gz
0b0aa0f4f0ec0c1f9fe0d2cce606dac7bd11b6ff4da801958630d6e0d4475d9f5a74511b03b6dd8aa20b9918b915b4c1584b9a6cd4f984cedce9f8b65043c68c  pythondata-cpu-minerva-2024.04.tar.gz
7258c9bbf12d889ed297d2883c9d20e4da0c9b105c963fe5cb61952326514951240c007df55579b11956b976244453a1a9dc4a3b3bf3d5e353b3fb1497afec7e  pythondata-cpu-mor1kx-2024.04.tar.gz
d581b1ac8ba6b09e5ac194ed8f9ce405fc862a5c120cba7648c60d584642c10ec6070f4b19a082bc024758f4e93540071a5561b18caf823ed40a675a163991dd  pythondata-cpu-naxriscv-2024.04.tar.gz
c24620f5be6ce09eac06ff1b1efed4a7e58a1cd0044dcc16894e6cc902a37ad82e9705bdd3598b0623c66ac8b7af3edc573d8502238d39a0641e1109043456a6  pythondata-cpu-picorv32-2024.04.tar.gz
77607b3e4e5194b3b817a2ce5fcabac6c1695d5a686f0a299a4c82197122d5dc3ad845409ea121c8eb56815f9ee5d704dccdf13f0fa1a5565550d0a57ce0cf3d  pythondata-cpu-rocket-2024.04.tar.gz
9e698309c24694706f951beafe94ede1abb11f9b4a24c89026bddad9d82ae0ad12a62fac25c9d19a9e0cb676eb934e3124d1984911d6cb99d0e93fb1ea76891e  pythondata-cpu-serv-2024.04.tar.gz
c2db22198b9b1ca3c75292314984f21a3f9f07436261cbb7b8d24f0e7afadbdc04f4ce10e6b6e9528881452421fe1a28c61791b75851d4060eadca7ab5073329  pythondata-cpu-vexriscv-2024.04.tar.gz
4e25f107f3da2407a68e1d77abb674a2c74e02729716280c5456dfe2cb895af0515c6ec8cc776f63da397fe0859d00c69085306af99d4571ce5900376711b767  pythondata-cpu-vexriscv_smp-2024.04.tar.gz
76ffae834f5090dae548d71eef6b0aa841237608010e7bc139d51465028144eb5aa3026573ac88735105c58d4851e3ab72609c810e2db18d096da5031a87df11  pythondata-misc-tapcfg-2024.04.tar.gz
9f82c8c54c7e11e3143e965cbefb4e7d5908cf6fbec9ca092724610c380998ed26aba41dea35b66b7653f8d4f96a32986f86a7b3d1d736a4b2c64d6f9a36823d  pythondata-software-compiler_rt-2024.04.tar.gz
bdf3186b5e0e55d3bcf73bde2b0a4e1803e4c8a0cb1ca9e144f04f41121f9270e619924742526f0640bf9a17b7035482a2ce8bad2adba9de8a8b1a38ce6f7395  pythondata-software-picolibc-2024.04.tar.gz
b9cb43acd169e8e05560ee5d90326a95426e26f8f837c21d3965770f8ed5fdb4352729d4f8320d8070e57fe36e3276d3b04df90498e874d6a6c3fab61fa85829  valentyusb-2023.12.tar.gz
"
